package com.yunkaweilai.android.activity.shop;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import com.classic.common.MultipleStatusView;
import com.flyco.animation.BaseAnimatorSet;
import com.flyco.animation.BounceEnter.BounceTopEnter;
import com.flyco.animation.SlideExit.SlideBottomExit;
import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.widget.MaterialDialog;
import com.google.gson.Gson;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.adapter.SlideAdapter;
import com.yunkaweilai.android.base.BaseActivity;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.constants.CommonConstants;
import com.yunkaweilai.android.interfaces.ListItemClick;
import com.yunkaweilai.android.model.Event;
import com.yunkaweilai.android.model.shop.CompanyTypeModel;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.TitleBuilder;
import com.yunkaweilai.android.utils.Utils;
import com.yunkaweilai.android.view.dialog.shop.ShopCompanyAddDialog;
import com.yunkaweilai.android.view.slider.SilderListView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 单位选择  有删除功能
 * Created by Administrator on 2017/6/15.
 */

public class CompanyTypeActivity extends BaseActivity {
    @BindView(R.id.id_multipleStatusView)
    MultipleStatusView idMultipleStatusView;
    @BindView(R.id.id_listview)
    SilderListView idListview;

    private CompanyTypeModel companyTypeModel;
    private ArrayList<CompanyTypeModel.DataBean.ListBean> companyList = new ArrayList<>();
    public static final String UNITID = "unitiD";
    public static final String UNIT_NAME = "UNIT_NAME";
    private SlideAdapter slideAdapter;
    private BaseAnimatorSet mBasIn;
    private BaseAnimatorSet mBasOut;
    private ShopCompanyAddDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_type_company);
        ButterKnife.bind(this);
        new TitleBuilder(this).setLeftImage(R.mipmap.ic_go_back)
                .setTitleText("单位选取").setRightText("新增")
                .setRightOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //新增单位
                        dialog = new ShopCompanyAddDialog(mAc);
                        dialog.show();
                    }
                });
        idMultipleStatusView.showLoading();
        idMultipleStatusView.setOnRetryClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initHttp();
            }
        });
        mBasIn = new BounceTopEnter();
        mBasOut = new SlideBottomExit();
        initAdapter();
        initClick();
        initHttp();
        //注册事件
        if (!EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().register(this);
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //取消注册事件
        if (EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().unregister(this);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onTypeEvent(Event.TypeEvent typeEvent) {
        if (typeEvent.type == CommonConstants.SHOP_COMPANY_REFRESH) {
            if (typeEvent.flag) {
                idMultipleStatusView.showLoading();
                initHttp();
            }
        }
    }


    private void initHttp() {
        HttpRequest.postUrl(Api.COMPANY_TYPE_LIST)
                .log(TAG).execute(new PostProcess.StringCallBack() {
            @Override
            public void onResponse(String json) {
                Gson gson = new Gson();
                boolean checkCode = Utils.checkCode(CompanyTypeActivity.this, json);
                if (checkCode) {
                    companyTypeModel = gson.fromJson(json, CompanyTypeModel.class);
                    companyList.clear();
                    companyList.addAll(companyTypeModel.getData().getList());
                    slideAdapter.notifyDataSetChanged();
                    if (idMultipleStatusView != null){
                        idMultipleStatusView.showContent();
                    }
                } else {
                    if (idMultipleStatusView != null){
                        idMultipleStatusView.showError();
                    }
                }
            }

            @Override
            public void onError(Exception e) {
                if (idMultipleStatusView != null){
                    idMultipleStatusView.showError();
                }
            }
        });
    }

    private void initClick() {
        idListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.putExtra(UNITID, companyList.get(position).getId());
                intent.putExtra(UNIT_NAME, companyList.get(position).getUnit_name());
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }

    private void initAdapter() {
        slideAdapter = new SlideAdapter(this,companyList);
        idListview.setAdapter(slideAdapter);

        slideAdapter.setOnClick(new ListItemClick() {
            @Override
            public void onClick(final int position) {
                //用户点击了删除
                final MaterialDialog dialog = new MaterialDialog(mAc);
                dialog.isTitleShow(false)//
                        .btnNum(2)
                        .content("是否删除该单位")//
                        .btnText("确定", "取消")//
                        .showAnim(mBasIn)//
                        .dismissAnim(mBasOut)//
                        .show();
                dialog.setOnBtnClickL(new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {//确定
                        delCompany(companyList.get(position).getId()+"",position);
                        dialog.dismiss();
                    }
                }, new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {//取消
                        dialog.dismiss();
                    }
                });
            }
        });
    }

    //删除单位
    private void delCompany(String id, final int position){
        HttpRequest.postUrl(Api.DEL_COMPANY_SHOP).addParams("id",id)
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(mAc, json);
                        if (checkCode){
                            companyList.remove(position);
                            slideAdapter.notifyDataSetChanged();
                            showToast("操作成功");
                        }
                    }

                    @Override
                    public void onError(Exception e) {

                    }
                });
    }


    @OnClick(R.id.titlebar_iv_left)
    public void onViewClicked() {
        finish();
    }
}
